
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电商用户消费倾向预测系统</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .header {
            text-align: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            border-radius: 10px;
            margin-bottom: 30px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .header h1 {
            margin: 0;
            font-size: 2.5em;
        }
        
        .header p {
            margin: 10px 0 0 0;
            font-size: 1.2em;
            opacity: 0.9;
        }
        
        .tab-container {
            background: white;
            border-radius: 10px;
            margin-bottom: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .tab-nav {
            display: flex;
            background-color: #f8f9fa;
        }
        
        .tab-button {
            flex: 1;
            padding: 15px 20px;
            border: none;
            background: transparent;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            border-bottom: 3px solid transparent;
        }
        
        .tab-button.active {
            background: white;
            border-bottom-color: #667eea;
            color: #667eea;
            font-weight: bold;
        }
        
        .tab-content {
            padding: 30px;
            display: none;
        }
        
        .tab-content.active {
            display: block;
        }
        
        .prediction-form {
            max-width: 800px;
            margin: 0 auto;
        }
        
        .form-section {
            margin-bottom: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
        }
        
        .form-section h3 {
            margin-top: 0;
            color: #333;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 15px;
            margin-bottom: 15px;
        }
        
        .form-group {
            display: flex;
            flex-direction: column;
        }
        
        .form-group label {
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        
        .form-group input, .form-group select {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .form-group input:focus, .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        }
        
        .form-group .hint {
            font-size: 12px;
            color: #888;
            margin-top: 3px;
        }
        
        .predict-button {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: bold;
            cursor: pointer;
            transition: transform 0.2s ease;
            margin-top: 20px;
        }
        
        .predict-button:hover {
            transform: translateY(-2px);
        }
        
        .predict-button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }
        
        .prediction-result {
            margin-top: 30px;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            display: none;
        }
        
        .prediction-result.show {
            display: block;
        }
        
        .prediction-result.high {
            background: linear-gradient(135deg, #ff6b6b 0%, #ff8e8e 100%);
            color: white;
        }
        
        .prediction-result.medium {
            background: linear-gradient(135deg, #feca57 0%, #ffd32a 100%);
            color: #333;
        }
        
        .prediction-result.low {
            background: linear-gradient(135deg, #48dbfb 0%, #0abde3 100%);
            color: white;
        }
        
        .prediction-result h3 {
            margin: 0 0 10px 0;
            font-size: 1.8em;
        }
        
        .prediction-result .confidence {
            font-size: 1.2em;
            opacity: 0.9;
        }
        
        .prediction-result .recommendation {
            margin-top: 15px;
            padding: 15px;
            background: rgba(255,255,255,0.2);
            border-radius: 5px;
            font-size: 14px;
        }
        
        .controls {
            background: white;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .button-group {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 15px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            background: #667eea;
            color: white;
        }
        
        .btn:hover {
            background: #5a67d8;
            transform: translateY(-2px);
        }
        
        .btn.active {
            background: #48bb78;
        }
        
        .visualization-container {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        
        .visualization {
            text-align: center;
            display: none;
        }
        
        .visualization.active {
            display: block;
        }
        
        .visualization img {
            max-width: 100%;
            height: auto;
            border-radius: 5px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin: 20px 0;
        }
        
        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .stat-card h4 {
            margin: 0 0 10px 0;
            color: #667eea;
            font-size: 1.1em;
        }
        
        .stat-card .value {
            font-size: 2em;
            font-weight: bold;
            color: #333;
        }
        
        .footer {
            text-align: center;
            margin-top: 40px;
            padding: 20px;
            color: #666;
        }
        
        @media (max-width: 768px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            
            .tab-nav {
                flex-direction: column;
            }
            
            .button-group {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🛒 电商用户消费倾向预测系统</h1>
        <p>基于机器学习的智能用户画像与消费预测</p>
    </div>
    
    <!-- 标签页容器 -->
    <div class="tab-container">
        <div class="tab-nav">
            <button class="tab-button active" onclick="showTab('prediction')">
                🎯 消费倾向预测
            </button>
            <button class="tab-button" onclick="showTab('analysis')">
                📊 模型分析
            </button>
        </div>
        
        <!-- 预测功能标签页 -->
        <div id="prediction" class="tab-content active">
            <div class="prediction-form">
                <h2 style="text-align: center; color: #333; margin-bottom: 30px;">
                    输入用户特征数据，预测消费倾向
                </h2>
                
                <form id="predictionForm">
                    <!-- 基础信息 -->
                    <div class="form-section">
                        <h3>👤 基础信息</h3>
                        <div class="form-row">
                            <div class="form-group">
                                <label for="age">年龄</label>
                                <input type="number" id="age" name="age" min="18" max="70" value="30" required>
                                <span class="hint">范围: 18-70岁</span>
                            </div>
                            <div class="form-group">
                                <label for="gender">性别</label>
                                <select id="gender" name="gender" required>
                                    <option value="">请选择</option>
                                    <option value="Male">男性</option>
                                    <option value="Female">女性</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="income">年收入 (元)</label>
                                <input type="number" id="income" name="income" min="20000" max="200000" value="60000" required>
                                <span class="hint">范围: 20,000-200,000元</span>
                            </div>
                            <div class="form-group">
                                <label for="city_tier">城市等级</label>
                                <select id="city_tier" name="city_tier" required>
                                    <option value="">请选择</option>
                                    <option value="Tier1">一线城市</option>
                                    <option value="Tier2">二线城市</option>
                                    <option value="Tier3">三线城市</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 消费行为 -->
                    <div class="form-section">
                        <h3>💳 消费行为</h3>
                        <div class="form-row">
                            <div class="form-group">
                                <label for="total_orders">历史订单数</label>
                                <input type="number" id="total_orders" name="total_orders" min="0" max="100" value="8" required>
                                <span class="hint">累计订单数量</span>
                            </div>
                            <div class="form-group">
                                <label for="total_spending">历史消费总额 (元)</label>
                                <input type="number" id="total_spending" name="total_spending" min="100" max="50000" value="2000" required>
                                <span class="hint">累计消费金额</span>
                            </div>
                            <div class="form-group">
                                <label for="favorite_category">偏好类别</label>
                                <select id="favorite_category" name="favorite_category" required>
                                    <option value="">请选择</option>
                                    <option value="Electronics">电子产品</option>
                                    <option value="Clothing">服装</option>
                                    <option value="Home">家居</option>
                                    <option value="Books">图书</option>
                                    <option value="Sports">运动</option>
                                    <option value="Beauty">美妆</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="payment_method">支付方式</label>
                                <select id="payment_method" name="payment_method" required>
                                    <option value="">请选择</option>
                                    <option value="Credit_Card">信用卡</option>
                                    <option value="Debit_Card">借记卡</option>
                                    <option value="Digital_Wallet">数字钱包</option>
                                    <option value="COD">货到付款</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 浏览行为 -->
                    <div class="form-section">
                        <h3>🖱️ 浏览行为</h3>
                        <div class="form-row">
                            <div class="form-group">
                                <label for="session_count">月访问次数</label>
                                <input type="number" id="session_count" name="session_count" min="0" max="50" value="15" required>
                                <span class="hint">每月网站访问次数</span>
                            </div>
                            <div class="form-group">
                                <label for="page_views">月浏览页面数</label>
                                <input type="number" id="page_views" name="page_views" min="0" max="200" value="50" required>
                                <span class="hint">每月浏览页面总数</span>
                            </div>
                            <div class="form-group">
                                <label for="time_on_site">平均停留时间 (分钟)</label>
                                <input type="number" id="time_on_site" name="time_on_site" min="5" max="180" value="30" required>
                                <span class="hint">每次访问平均时长</span>
                            </div>
                            <div class="form-group">
                                <label for="registration_days">注册天数</label>
                                <input type="number" id="registration_days" name="registration_days" min="1" max="2000" value="365" required>
                                <span class="hint">账户注册至今天数</span>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group">
                                <label for="cart_abandonment_rate">购物车放弃率</label>
                                <input type="number" id="cart_abandonment_rate" name="cart_abandonment_rate" min="0" max="1" step="0.1" value="0.3" required>
                                <span class="hint">0-1之间的小数</span>
                            </div>
                            <div class="form-group">
                                <label for="wishlist_items">收藏商品数</label>
                                <input type="number" id="wishlist_items" name="wishlist_items" min="0" max="20" value="3" required>
                                <span class="hint">当前收藏的商品数量</span>
                            </div>
                            <div class="form-group">
                                <label for="review_count">评价次数</label>
                                <input type="number" id="review_count" name="review_count" min="0" max="20" value="2" required>
                                <span class="hint">历史评价总数</span>
                            </div>
                        </div>
                    </div>
                    
                    <button type="submit" class="predict-button" id="predictBtn">
                        🔮 预测消费倾向
                    </button>
                </form>
                
                <!-- 预测结果 -->
                <div id="predictionResult" class="prediction-result">
                    <h3 id="resultTitle"></h3>
                    <div id="resultConfidence" class="confidence"></div>
                    <div id="resultRecommendation" class="recommendation"></div>
                </div>
            </div>
        </div>
        
        <!-- 模型分析标签页 -->
        <div id="analysis" class="tab-content">
            <!-- 统计卡片 -->
            <div class="stats-grid">
                <div class="stat-card">
                    <h4>最佳模型</h4>
                    <div class="value">LR</div>
                </div>
                <div class="stat-card">
                    <h4>模型准确率</h4>
                    <div class="value">0.933</div>
                </div>
                <div class="stat-card">
                    <h4>训练样本数</h4>
                    <div class="value">1000</div>
                </div>
                <div class="stat-card">
                    <h4>特征维度</h4>
                    <div class="value">80</div>
                </div>
            </div>
            
            <!-- 可视化控制面板 -->
            <div class="controls">
                <h3>📊 选择可视化类型</h3>
                <div class="button-group">
                    <button class="btn active" onclick="showVisualization('model_performance')">
                        📈 模型性能对比
                    </button>
                    <button class="btn" onclick="showVisualization('confusion_matrix')">
                        🎯 混淆矩阵
                    </button>
                    <button class="btn" onclick="showVisualization('data_distribution')">
                        📊 数据分布
                    </button>
                    <button class="btn" onclick="showVisualization('feature_importance')">
                        🔍 特征重要性
                    </button>
                    <button class="btn" onclick="showVisualization('training_time')">
                        ⏱️ 训练时间
                    </button>
                </div>
            </div>
            
            <!-- 可视化容器 -->
            <div class="visualization-container">
                <div id="model_performance" class="visualization active">
                    <h3>📈 模型性能对比</h3>
                    <img src="" alt="模型性能对比">
                    <p style="color: #666; margin-top: 15px;">
                        展示了所有训练模型在准确率、精确率、召回率和F1分数上的表现对比
                    </p>
                </div>
                
                <div id="confusion_matrix" class="visualization">
                    <h3>🎯 混淆矩阵 - Basic LR</h3>
                    <img src="" alt="混淆矩阵">
                    <p style="color: #666; margin-top: 15px;">
                        显示最佳模型在各消费倾向类别上的预测准确性
                    </p>
                </div>
                
                <div id="data_distribution" class="visualization">
                    <h3>📊 数据分布分析</h3>
                    <img src="" alt="数据分布">
                    <p style="color: #666; margin-top: 15px;">
                        展示了用户年龄、收入、消费倾向等级和总消费的分布情况
                    </p>
                </div>
                
                <div id="feature_importance" class="visualization">
                    <h3>🔍 特征重要性分析</h3>
                    <img src="" alt="特征重要性">
                    <p style="color: #666; margin-top: 15px;">
                        基于TF-IDF分析的最重要特征
                    </p>
                </div>
                
                <div id="training_time" class="visualization">
                    <h3>⏱️ 模型训练时间对比</h3>
                    <img src="" alt="训练时间">
                    <p style="color: #666; margin-top: 15px;">
                        比较不同模型的训练时间
                    </p>
                </div>
            </div>
        </div>
    </div>
    
    <div class="footer">
        <p>🚀 电商用户消费倾向预测系统 | 智能预测，精准营销</p>
    </div>
    
    <script>
        // 标签页切换功能
        function showTab(tabName) {
            // 隐藏所有标签页内容
            const tabContents = document.querySelectorAll('.tab-content');
            tabContents.forEach(content => {
                content.classList.remove('active');
            });
            
            // 显示选中的标签页
            document.getElementById(tabName).classList.add('active');
            
            // 更新标签按钮状态
            const tabButtons = document.querySelectorAll('.tab-button');
            tabButtons.forEach(button => {
                button.classList.remove('active');
            });
            
            // 设置当前按钮为活跃状态
            event.target.classList.add('active');
        }
        
        // 可视化切换功能
        function showVisualization(vizId) {
            const visualizations = document.querySelectorAll('.visualization');
            visualizations.forEach(viz => {
                viz.classList.remove('active');
            });
            
            document.getElementById(vizId).classList.add('active');
            
            const buttons = document.querySelectorAll('.btn');
            buttons.forEach(btn => {
                btn.classList.remove('active');
            });
            
            event.target.classList.add('active');
        }
        
        // 预测功能
        function predictConsumption(formData) {
            // 这是一个简化的预测逻辑
            // 实际应用中需要调用后端API
            
            // 计算特征得分
            const age = parseInt(formData.age);
            const income = parseInt(formData.income);
            const totalSpending = parseInt(formData.total_spending);
            const totalOrders = parseInt(formData.total_orders);
            const sessionCount = parseInt(formData.session_count);
            
            // 标准化分数 (简化版本)
            let score = 0;
            
            // 年龄因子 (中年用户消费能力较强)
            if (age >= 30 && age <= 50) {
                score += 0.2;
            } else if (age >= 25 && age <= 55) {
                score += 0.1;
            }
            
            // 收入因子
            if (income >= 100000) {
                score += 0.3;
            } else if (income >= 60000) {
                score += 0.2;
            } else if (income >= 40000) {
                score += 0.1;
            }
            
            // 历史消费因子
            if (totalSpending >= 10000) {
                score += 0.25;
            } else if (totalSpending >= 5000) {
                score += 0.15;
            } else if (totalSpending >= 2000) {
                score += 0.1;
            }
            
            // 购买频率因子
            const avgOrderValue = totalSpending / Math.max(totalOrders, 1);
            if (avgOrderValue >= 500) {
                score += 0.15;
            } else if (avgOrderValue >= 200) {
                score += 0.1;
            }
            
            // 活跃度因子
            if (sessionCount >= 20) {
                score += 0.1;
            } else if (sessionCount >= 10) {
                score += 0.05;
            }
            
            // 城市等级因子
            if (formData.city_tier === 'Tier1') {
                score += 0.1;
            } else if (formData.city_tier === 'Tier2') {
                score += 0.05;
            }
            
            // 偏好类别因子
            if (['Electronics', 'Beauty'].includes(formData.favorite_category)) {
                score += 0.05;
            }
            
            // 确定预测结果
            let prediction, confidence, recommendation;
            
            if (score >= 0.7) {
                prediction = 'High';
                confidence = Math.min(85 + Math.random() * 10, 95);
                recommendation = `
                    <strong>高消费倾向用户</strong><br>
                    • 推荐高价值产品和服务<br>
                    • 提供VIP专属优惠<br>
                    • 重点维护客户关系<br>
                    • 推送新品和限量版商品
                `;
            } else if (score >= 0.4) {
                prediction = 'Medium';
                confidence = Math.min(75 + Math.random() * 10, 85);
                recommendation = `
                    <strong>中等消费倾向用户</strong><br>
                    • 提供性价比高的产品推荐<br>
                    • 适度的促销活动<br>
                    • 个性化商品推荐<br>
                    • 提升用户参与度
                `;
            } else {
                prediction = 'Low';
                confidence = Math.min(70 + Math.random() * 10, 80);
                recommendation = `
                    <strong>低消费倾向用户</strong><br>
                    • 推荐基础款和优惠商品<br>
                    • 提供新用户专享折扣<br>
                    • 培养购物习惯<br>
                    • 关注用户体验提升
                `;
            }
            
            return {
                prediction: prediction,
                confidence: confidence.toFixed(1),
                recommendation: recommendation
            };
        }
        
        // 表单提交处理
        document.getElementById('predictionForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const formData = new FormData(this);
            const data = Object.fromEntries(formData.entries());
            
            // 显示加载状态
            const predictBtn = document.getElementById('predictBtn');
            const originalText = predictBtn.textContent;
            predictBtn.textContent = '🔄 预测中...';
            predictBtn.disabled = true;
            
            // 模拟预测延迟
            setTimeout(() => {
                // 执行预测
                const result = predictConsumption(data);
                
                // 显示结果
                showPredictionResult(result);
                
                // 恢复按钮状态
                predictBtn.textContent = originalText;
                predictBtn.disabled = false;
            }, 1500);
        });
        
        function showPredictionResult(result) {
            const resultDiv = document.getElementById('predictionResult');
            const titleDiv = document.getElementById('resultTitle');
            const confidenceDiv = document.getElementById('resultConfidence');
            const recommendationDiv = document.getElementById('resultRecommendation');
            
            // 设置结果内容
            const predictionTexts = {
                'High': '🔥 高消费倾向',
                'Medium': '⚡ 中等消费倾向', 
                'Low': '💧 低消费倾向'
            };
            
            titleDiv.textContent = predictionTexts[result.prediction];
            confidenceDiv.textContent = `预测置信度: ${result.confidence}%`;
            recommendationDiv.innerHTML = result.recommendation;
            
            // 设置样式
            resultDiv.className = `prediction-result show ${result.prediction.toLowerCase()}`;
            
            // 滚动到结果区域
            resultDiv.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
        
        // 页面加载动画
        window.addEventListener('load', function() {
            const cards = document.querySelectorAll('.stat-card');
            cards.forEach((card, index) => {
                setTimeout(() => {
                    card.style.opacity = '0';
                    card.style.transform = 'translateY(20px)';
                    card.style.transition = 'all 0.5s ease';
                    
                    setTimeout(() => {
                        card.style.opacity = '1';
                        card.style.transform = 'translateY(0)';
                    }, 100);
                }, index * 150);
            });
        });
        
        // 自动计算派生特征
        function updateDerivedFeatures() {
            const totalOrders = parseInt(document.getElementById('total_orders').value) || 0;
            const totalSpending = parseInt(document.getElementById('total_spending').value) || 0;
            const registrationDays = parseInt(document.getElementById('registration_days').value) || 1;
            
            // 这里可以添加实时计算和显示逻辑
            // 例如显示平均订单价值、购买频率等
        }
        
        // 为数值输入添加事件监听
        document.querySelectorAll('input[type="number"]').forEach(input => {
            input.addEventListener('input', updateDerivedFeatures);
        });
    </script>
</body>
</html>
